//数字滚动
;(function($, window, document) {
    "use strict";
    var defaults = {
        deVal: 0,       //传入值
        className:'',   //样式名称
        digit:''    //默认显示几位数字
    };
    function rollNum(obj, options){
        this.obj = obj;
        this.options = $.extend(defaults, options);
        this.init = function(){
             this.initHtml(obj,defaults);
        }
    }
    rollNum.prototype = {
        initHtml: function(obj,options){
            var strHtml = '<ul class="dataNums ' + options.className + ' inrow">';
            if(options.transToThursand && options.deVal >= 1000){
                var newNumArr = (options.deVal+'').split('').reverse();
                $.each(newNumArr,function(i,item){
                    if((i+1) % 3 == 0 && i!=newNumArr.length-1 && i!=(newNumArr.indexOf('.'))){
                        newNumArr[i] =  (','+item);
                    }
                })
                options.deVal  = newNumArr.reverse().join('');
            }
            var valLen = options.digit ||  (options.deVal + '').length;
            obj.html('');
            if(obj.find('.dataNums'+options.className).length <= 0){
                for(var i = 0; i<  valLen; i++){
                    strHtml += '<li class="dataOne "><div class="dataBoc"><div class="tt" t="38"><span class="number num0">0</span> <span class="number num1">1</span> <span class="number num2">2</span> <span class="number num3">3</span> <span class="number num4">4</span><span class="number num5">5</span> <span class="number num6">6</span> <span class="number num7">7</span> <span class="number num8">8</span> <span class="number num9">9</span><span class="number num0">0</span> <span class="number num1">1</span> <span class="number num2">2</span> <span class="number num3">3</span> <span class="number num4">4</span><span class="number num5">5</span> <span class="number num6">6</span> <span class="number num7">7</span> <span class="number num8">8</span> <span class="number num9">9</span></div></div></li>';
                }
                strHtml += '</ul>';
                obj.html(strHtml);
            }
            this.scroNum(obj, options);

        },
        scroNum: function(obj, options){
            var number = options.deVal;
			var $num_item = $(obj).find('.dataNums' + options.className).find('.tt');
            var h = $(obj).find('.dataBoc .number')[0].getBoundingClientRect().height; 
            if(!options.notScroll){
                $num_item.css('transition','all .75s ease-in-out');
            }
            var numberStr = number.toString();
            if(numberStr.length <= $num_item.length - 1){
                var tempStr = '';
                for(var a = 0; a < $num_item.length - numberStr.length; a++){
                    tempStr += '0';
                }
                numberStr = tempStr + numberStr;
            }

            var numberArr = numberStr.split('');
            
            $num_item.each(function(i, item) {
                setTimeout(function(){
                    $num_item.eq(i).css('top',-parseFloat(numberArr[i])*h);
                    if (numberArr[i] == '.' || numberArr[i] == ',') {
                        $num_item.eq(i).prepend('<span class="num.">'+(numberArr[i])+'</span>');
                        $num_item.eq(i).parents('.dataOne').width('.6vh');
                        $num_item.eq(i).css({
                            top: '-1px'
                        });
                    }
                },i*100)
            });
        }
    }
    $.fn.rollNum = function(options){
        var $that = this;
        var rollNumObj = new rollNum($that, options);
        rollNumObj.init();
    };
})(jQuery, window, document);